package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;

    /*
        更新游记
        动作:PUT
        资源:/travels/{id}
        参数:xx=xx&...
     */
    @PutMapping("/{id}")
    public Object update(Travel entity) {
        travelService.saveOrUpdate(entity);
        return new JSONResult();
    }

    /*
        新增游记
        动作:POST
        资源:/travels
        参数:xx=xx&...
     */
    @PostMapping
    public Object save(Travel entity) {
        travelService.saveOrUpdate(entity);
        return new JSONResult();
    }

    /*
        查询某篇游记的信息
        动作:GET
        资源:/travels/{id}
        参数:
     */
    @GetMapping("/{id}")
    public Object getTravel(@PathVariable Long id) {
        return travelService.get(id);
    }

    /*
        获取所有公开审核通过的游记
        动作:GET
        资源:/travels
        参数:
     */
    @GetMapping
    public Object queryTravels(TravelQueryObject qo) {
        //设置只查询已发布并且公开
        qo.setState(Travel.STATE_RELEASE);
        //设置按照创建时间降序排列
        qo.setOrderBy("t.createTime desc");
        return travelService.queryTravels(qo);
    }
    @GetMapping("/userTravels")
    public Object userTravels(TravelQueryObject qo) {
        //设置只查询已发布并且公开
        qo.setState(Travel.STATE_RELEASE);
        //设置按照创建时间降序排列
        qo.setPageSize(0);
        qo.setOrderBy("t.createTime desc");
        return travelService.queryTravels(qo);
    }
    //好友动态
    @GetMapping("/friendTravel")
    public Object friendTravel(TravelQueryObject qo) {
        qo.setAuthorId(UserContext.getCurrentUser().getId());
        //设置只查询已发布并且公开
        qo.setState(Travel.STATE_RELEASE);
        //设置按照创建时间降序排列
        qo.setOrderBy("t.createTime desc");
        return travelService.queryfriendTravel(qo);
    }

    /*
        查询推荐的游记
        动作:GET
        资源:/travels/commends
        参数:type=xx
     */
    @GetMapping("/commends")
    public Object listCommends(Integer type) {
        /*//设置按照推荐时间降序排列
        qo.setOrderBy("schedule desc");*/
        return travelCommendService.listCommends(type);
    }
    /**
     * 动作:GET
     * 资源:/travels/titles/strategyId
     * 参数:
     */

    @GetMapping("/titles")
    public Object listTravelTitle(Long strategyId){
        return travelService.listTravelTitle(strategyId);
    }
    //查询我被收藏的的游记且未读条数
    @GetMapping("/enshrine")
    public Object enshrine() {
        Long id = UserContext.getCurrentUser().getId();
        return travelService.enshrineNumber(id);
    }
    @GetMapping("/collectMe")
    public Object collectMe() {
        List<Map<String,Object>> list= travelService.ForcollectMe(UserContext.getCurrentUser().getId());
        return list;
    }
    @GetMapping("/likeMe")
    public Object likeMe() {
        List<Map<String,Object>> list= travelService.likeMe(UserContext.getCurrentUser().getId());
        return list;
    }
    @GetMapping("/likeNumber")
    public Object likeNumber() {
        Map<String,Object> map= travelService.likeNumber(UserContext.getCurrentUser().getId());
        return map;
    }
}
